package com.sens.orderback.ui.activity.alipay;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sens.common.DeviceUtil;
import com.sens.commonui.PermissionUtil;
import com.sens.orderback.OrderBackApplication;
import com.sens.orderback.R;
import com.sens.orderback.ui.base.BaseFragment;
import com.sens.utils.AlipayTestHelper;

public class AlipayFragment extends BaseFragment {
    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_alipay_main, null);
    }

    @Override
    public void onFirstUserVisible() {
        titleTV = findViewById(R.id.titleTV);
        scrollView = findViewById(R.id.scrollView);
        textView = findViewById(R.id.textView);
        findViewById(R.id.goSetting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goSetting(view);
            }
        });
        findViewById(R.id.history).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                history(view);
            }
        });
        findViewById(R.id.clearLog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearLog(view);
            }
        });
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                test(view);
            }
        });
        initTestHelper();
    }

    private ScrollView scrollView;
    private TextView textView;
    private TextView titleTV;

    private AlipayTestHelper alipayTestHelper;

    private Runnable scrollRunnable = new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    };
    private boolean hasDeviceId = false;
    private boolean hasDatabase = false;

    public boolean initTestHelper() {
        if (!hasDeviceId)
            DeviceUtil.getDeviceId(getActivity(), new DeviceUtil.OnDeviceIdCallback() {
                @Override
                protected void onDeviceIdCallback(String deviceId) {
                    hasDeviceId = true;
                    titleTV.setText(DeviceUtil.getSystemModel() + ":" + deviceId);
                }
            });
        if (!hasDatabase)
            if (!PermissionUtil.checkPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                PermissionUtil.requestPermissions(getActivity(), new PermissionUtil.OnPermissionCallback() {
                    @Override
                    public void onGranted() {
                        if (!hasDatabase) initDatabase();
                    }
                }, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else initDatabase();
        return hasDeviceId && hasDatabase;
    }

    @Override
    public void onResume() {
        super.onResume();
        initTestHelper();
    }

    private void initDatabase() {
        hasDatabase = true;
        OrderBackApplication.getInstance().initDatabase();
        alipayTestHelper = new AlipayTestHelper(getActivity(), new AlipayTestHelper.NoticeLogListener() {
            @Override
            public void noticeLog(CharSequence message) {
                textView.setText(message);
                scrollView.postDelayed(scrollRunnable, 100);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (alipayTestHelper != null)
            alipayTestHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {
        if (alipayTestHelper != null) alipayTestHelper.saveLog();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (alipayTestHelper != null) alipayTestHelper.unBind();
        scrollView = null;
        textView = null;
        titleTV = null;
        alipayTestHelper = null;
        scrollRunnable = null;
        super.onDestroy();
    }

    public void goSetting(View view) {
        startActivity(new Intent(getActivity(), SettingActivity.class));
    }

    public void history(View view) {
        startActivity(new Intent(getActivity(), ListActivity.class));
    }

    public void clearLog(View view) {
        if (!initTestHelper()) return;
        if (alipayTestHelper != null) alipayTestHelper.clearLog();
    }

    public void test(View view) {
        if (!initTestHelper()) return;
        if (alipayTestHelper != null) alipayTestHelper.startTest();
    }
}
